//
//  ViewController.m
//  30103-动画移动
//
//  Created by student on 16/3/17.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic,assign) CGPoint clickPoint;
@end

@implementation ViewController
{
    CGFloat xx;
    CGFloat yy;
    NSTimer *t;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.imageView setImage:[UIImage imageNamed:@"chara1"]];
    NSArray *picArr = @[[UIImage imageNamed:@"chara1"],[UIImage imageNamed:@"chara2"]];
    self.imageView.animationImages = picArr;
    self.imageView.animationDuration = 0.5;
    [self.imageView startAnimating];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    self.clickPoint = point;
    xx = self.clickPoint.x - self.imageView.center.x;
    yy = self.clickPoint.y - self.imageView.center.y;

    t = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(imageViewMove) userInfo:nil repeats:YES];
}

-(void)imageViewMove{
    self.imageView.center = CGPointMake(self.imageView.center.x + xx/25.0, self.imageView.center.y + yy/25.0);
    if (self.imageView.center.x == self.clickPoint.x && self.imageView.center.y == self.clickPoint.y) {
        [t invalidate];
    }
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [t invalidate];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
